﻿using System;
using System.Collections.Generic;
using System.Drawing.Printing;


namespace HualongExcel.Utilities
{
    public static class PrinterHelper
    {
        /// <summary>
        /// 获取本地已安装的打印机
        /// </summary>
        /// <returns></returns>
        public static string GetPrinter()
        {
            string strList = "";
            System.Drawing.Printing.PrinterSettings.StringCollection PrinterList = System.Drawing.Printing.PrinterSettings.InstalledPrinters;
            foreach (var item in PrinterList)
            {
                strList += item + ",";
            }
            return strList;
        }

        public static PrinterSettings GetPrinterBySelect()
        {
          
            var printDialog = new System.Windows.Forms.PrintDialog()
            {
                Document = new PrintDocument()
            };
            var dialogRet = printDialog.ShowDialog();
            if (dialogRet == System.Windows.Forms.DialogResult.OK || dialogRet == System.Windows.Forms.DialogResult.Yes)
            {
                return printDialog.Document.PrinterSettings;
            }
            return null;
        }



        /// <summary>
        /// 获取默认打印机
        /// </summary>
        /// <returns></returns>
        public static string GetDefault()
        {
            System.Drawing.Printing.PrintDocument p = new System.Drawing.Printing.PrintDocument();
            return p.DefaultPageSettings.PrinterSettings.PrinterName;
        }

        /// <summary>
        /// 获取可用的端口
        /// </summary>
        /// <returns></returns> 
        public static List<string> GetComList()
        {
            List<string> ComList = new List<string>();
            Microsoft.VisualBasic.Devices.Computer pc = new Microsoft.VisualBasic.Devices.Computer();
            foreach (string s in pc.Ports.SerialPortNames)
            {
                ComList.Add(s);
            }
            return ComList;
        }
    }
}
